use "data_analysis_final", clear

** Augmented Dickey-Fuller tests
xtunitroot  fisher red_t1_abs_disp,  dfuller lags(2) demean drift
xtunitroot  fisher red_m20_abs_disp, dfuller lags(2) demean drift
xtunitroot  fisher red_b20_abs_disp, dfuller lags(2) demean drift

xtunitroot  fisher red_t1_absls, dfuller lags(2) demean drift
xtunitroot  fisher red_m20_absls, dfuller lags(2) demean drift
xtunitroot  fisher red_b20_absls, dfuller lags(2) demean drift

xtunitroot  fisher red_t1_disp,  dfuller lags(2) demean drift
xtunitroot  fisher red_m20_disp, dfuller lags(2) demean drift
xtunitroot  fisher red_b20_disp, dfuller lags(2) demean drift

xtunitroot  fisher red_t1ls, dfuller lags(2) demean drift
xtunitroot  fisher red_m20ls, dfuller lags(2) demean drift
xtunitroot  fisher red_b20ls, dfuller lags(2) demean drift

xtunitroot  fisher top_inequality, dfuller lags(2) demean drift
xtunitroot  fisher bottom_inequality_b20, dfuller lags(2)  demean drift 
xtunitroot  fisher iopenc, dfuller lags(2) demean   drift
xtunitroot  fisher ikaopen, dfuller lags(2) demean drift
xtunitroot  fisher unemp, dfuller lags(2) demean drift
xtunitroot  fisher cum_left, dfuller lags(2)  demean drift
xtunitroot  fisher imarriage_rate, dfuller lags(2)  demean  drift
xtunitroot  fisher pb_ni, dfuller lags(2)  demean  drift

** Levin-Lin-Chu tests
* Contrary to the augmented Dickey-Fuller test, the LLC test requires a strongly balanced panel, so we trim the dataset to ensure balance before running the tests 
drop if year>2016
drop if year<1982
drop if country=="Austria" | country=="Belgium" 

xtunitroot  llc red_t1_abs_disp, lags(aic 3) demean
xtunitroot  llc red_t1_abs_disp, lags(aic 3) demean trend
xtunitroot  llc red_m20_abs_disp, lags(aic 3) demean
xtunitroot  llc red_m20_abs_disp, lags(aic 3) demean trend
xtunitroot  llc red_b20_abs_disp, lags(aic 3) demean
xtunitroot  llc red_b20_abs_disp, lags(aic 3) demean trend

xtunitroot  llc red_t1_absls, lags(aic 3) demean
xtunitroot  llc red_t1_absls, lags(aic 3) demean trend
xtunitroot  llc red_m20_absls, lags(aic 3) demean
xtunitroot  llc red_m20_absls, lags(aic 3) demean trend
xtunitroot  llc red_b20_absls, lags(aic 3) demean
xtunitroot  llc red_b20_absls, lags(aic 3) demean trend

xtunitroot  llc red_t1_disp, lags(aic 3) demean
xtunitroot  llc red_t1_disp, lags(aic 3) demean trend
xtunitroot  llc red_m20_disp, lags(aic 3) demean
xtunitroot  llc red_m20_disp, lags(aic 3) demean trend
xtunitroot  llc red_b20_disp, lags(aic 3) demean
xtunitroot  llc red_b20_disp, lags(aic 3) demean trend

xtunitroot  llc red_t1ls, lags(aic 3) demean
xtunitroot  llc red_t1ls, lags(aic 3) demean trend
xtunitroot  llc red_m20ls, lags(aic 3) demean 
xtunitroot  llc red_m20ls, lags(aic 3) demean trend 
xtunitroot  llc red_b20ls, lags(aic 3) demean
xtunitroot  llc red_b20ls, lags(aic 3) demean trend

xtunitroot  llc top_inequality, lags(aic 3) demean
xtunitroot  llc top_inequality, lags(aic 3) demean trend 
xtunitroot  llc bottom_inequality_b20, lags(aic 3)  demean
xtunitroot  llc bottom_inequality_b20, lags(aic 3)  demean  trend
xtunitroot  llc iopenc, lags(aic 3) demean
xtunitroot  llc iopenc, lags(aic 3) demean trend 
xtunitroot  llc ikaopen, lags(aic 3) demean 
xtunitroot  llc ikaopen, lags(aic 3) demean trend 
xtunitroot  llc cum_left, lags(aic 3) demean
xtunitroot  llc cum_left, lags(aic 3) demean trend
xtunitroot  llc unemp, lags(aic 3) demean 
xtunitroot  llc unemp, lags(aic 3) demean trend
xtunitroot  llc imarriage_rate, lags(aic 3)  demean 
xtunitroot  llc imarriage_rate, lags(aic 3)  demean trend 
xtunitroot  llc pb_ni, lags(aic 3)  demean 
xtunitroot  llc pb_ni, lags(aic 3)  demean trend 

* Next, we trim the dataset in an alternative way, omitting Austria, Belgium, and Switzerland, so we have a full time series between 1980 and 2016
use "data_analysis_final", clear

drop if year>2016
drop if country=="Austria" | country=="Belgium"  | country=="Switzerland"

xtunitroot  llc red_t1_abs_disp, lags(aic 3) demean
xtunitroot  llc red_t1_abs_disp, lags(aic 3) demean trend
xtunitroot  llc red_m20_abs_disp, lags(aic 3) demean
xtunitroot  llc red_m20_abs_disp, lags(aic 3) demean trend
xtunitroot  llc red_b20_abs_disp, lags(aic 3) demean
xtunitroot  llc red_b20_abs_disp, lags(aic 3) demean trend

xtunitroot  llc red_t1_absls, lags(aic 3) demean
xtunitroot  llc red_t1_absls, lags(aic 3) demean trend
xtunitroot  llc red_m20_absls, lags(aic 3) demean 
xtunitroot  llc red_m20_absls, lags(aic 3) demean trend 
xtunitroot  llc red_b20_absls, lags(aic 3) demean
xtunitroot  llc red_b20_absls, lags(aic 3) demean trend

xtunitroot  llc red_t1_disp, lags(aic 3) demean
xtunitroot  llc red_t1_disp, lags(aic 3) demean trend
xtunitroot  llc red_m20_disp, lags(aic 3) demean
xtunitroot  llc red_m20_disp, lags(aic 3) demean trend
xtunitroot  llc red_b20_disp, lags(aic 3) demean
xtunitroot  llc red_b20_disp, lags(aic 3) demean trend

xtunitroot  llc red_t1ls, lags(aic 3) demean
xtunitroot  llc red_t1ls, lags(aic 3) demean trend
xtunitroot  llc red_m20ls, lags(aic 3) demean 
xtunitroot  llc red_m20ls, lags(aic 3) demean trend 
xtunitroot  llc red_b20ls, lags(aic 3) demean
xtunitroot  llc red_b20ls, lags(aic 3) demean trend

xtunitroot  llc top_inequality, lags(aic 3) demean 
xtunitroot  llc top_inequality, lags(aic 3) demean trend 
xtunitroot  llc bottom_inequality_b20, lags(aic 3)  demean
xtunitroot  llc bottom_inequality_b20, lags(aic 3)  demean  trend
xtunitroot  llc iopenc, lags(aic 3) demean 
xtunitroot  llc iopenc, lags(aic 3) demean trend 
xtunitroot  llc cum_left, lags(aic 3) demean
xtunitroot  llc cum_left, lags(aic 3) demean trend
xtunitroot  llc unemp, lags(aic 3) demean 
xtunitroot  llc unemp, lags(aic 3) demean trend
xtunitroot  llc imarriage_rate, lags(aic 3)  demean 
xtunitroot  llc imarriage_rate, lags(aic 3)  demean trend 
xtunitroot  llc pb_ni, lags(aic 3)  demean 
xtunitroot  llc pb_ni, lags(aic 3)  demean trend 

* there is no data on ikaopen in NL1980. We drop this obs to get a balanced panel for the LLC test of ikaopen (capital account openness).
drop if year==1980

xtunitroot  llc ikaopen, lags(aic 3) demean 
xtunitroot  llc ikaopen, lags(aic 3) demean trend

** Tests for serial correlation:
* we use the "st0514" Stata package developed by Jesse Wursten. (Wursten, Jesse. 2018. "Testing for serial correlation in fixed-effects panel models" The Stata Journal 18, Number 1, pp. 76–100)

use "data_analysis_final", clear

xtset ccode year

* relative transfer rates

* one lagged DV
xtreg d1.red_t1_disp l1.red_t1_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp, fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* two lagged DV
xtreg d1.red_t1_disp l1.red_t1_disp l2.red_t1_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp, fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* one lagged DV
xtreg d1.red_t1ls l1.red_t1ls  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni, fe 
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* two lagged DV
xtreg d1.red_t1ls l1.red_t1ls l2.red_t1ls  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni, fe 
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* one lagged DV
xtreg d1.red_m20_disp l1.red_m20_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp , fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* two lagged DV
xtreg d1.red_m20_disp l1.red_m20_disp l2.red_m20_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp , fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* one lagged DV
xtreg d1.red_m20ls l1.red_m20ls d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc    d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni, fe 
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* two lagged DV
xtreg d1.red_m20ls l1.red_m20ls l2.red_m20ls d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc    d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni, fe 
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* one lagged DV
xtreg d1.red_b20_disp l1.red_b20_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp, fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)


* two lagged DV
xtreg d1.red_b20_disp l1.red_b20_disp l2.red_b20_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp, fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* one lagged DV
xtreg d1.red_b20ls l1.red_b20ls d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc    d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni, fe 
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

* two lagged DV
xtreg d1.red_b20ls l1.red_b20ls l2.red_b20ls d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc    d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni, fe 
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)


** absolute transfer rates:
xtreg d1.red_t1_abs_disp l1.red_t1_abs_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp, fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

xtreg d1.red_t1_absls l1.red_t1_absls   d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni , fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

xtreg d1.red_m20_abs_disp l1.red_m20_abs_disp d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp , fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

xtreg d1.red_m20_absls l1.red_m20_absls d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni, fe 
xtqptest, order(1)
xtqptest, lags(1) 
xtqptest, lags(2) 

xtreg d1.red_b20_abs_disp l1.red_b20_abs_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp , fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)

xtreg d1.red_b20_absls l1.red_b20_absls d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc    d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni , fe
xtqptest, order(1)
xtqptest, lags(1)
xtqptest, lags(2)
